************************************************************************************************************
** Stata replication file for "Image of China among the Taiwanese Public: Results from Two List Experiments"
** Chung-li Wu and Alex Min-Wei Lin                                                       
** All commands run using Stata 15
** Need to install the user-defined "kict" and "coefplot" packages       
************************************************************************************************************

// set working directory (from where you will upload/save data) //
*cd "your path/directory here" 

// install coefplot and kict packages //
ssc install coefplot, replace
net install st0559.pkg, from("http://www.stata-journal.com/software/sj19-2/") replace

// open file named "2019 March China Image Survey.dta" //
use "2019 March China Image Survey.dta", clear

set more off, permanently
set scheme cleanplots, perm


// VARIABLES IN DATA //
* Recode vsex (gender)
recode vsex (2=1) (1=0), gen (female)
label variable female "Gender (female=1)"
label define female 0 "Male" 1 "Female"
label values female female

* Recode vage
recode vage (1 2=1) (3 4=2) (5 6=3) (98=.), gen (age3)
label variable age3 "Age (3 groups)"
label define agegroups3 1 "20-39" 2 "40-59" 3 "60 and over"
label values age3 agegroups3

destring K1_0, gen(year)
gen age=107-year
label variable age "Age"

* Recode vedu (education)
recode vedu (1/2=1) (3=2) (4 5 6 =3) (98=.), gen (education2)
label variable education2 "Education"
label define edu2 1 "Junior high or below" 2 "High school" 3 "Some college and above"
label values education2 edu2

* Recode v39 (ethnicity)
recode v39 (1=1) (2=2) (3=3) (4/98=.), gen (ethnicity)
label variable ethnicity "Ethnicity"
label define ethnicity 1 "Hakka" 2 "Minnan" 3 "Mainlander"
label values ethnicity ethnicity

* Recode v38 (partyid)
recode v38 (1 4 5 10=1) (2 3 6 7 9 11=2) (12 13=3) (94/98=.), gen (partyid)
label variable partyid "Party identification"
label define partyid 1 "Pan-Blue" 2 "Pan-Green" 3 "Independent"
label values partyid partyid

* Recode v37 (natl_id)
recode v37 (1=1) (2=3) (3=2) (4/98=.), gen (natl_id)
label variable natl_id "National identity"
label define natl_id 1 "Taiwanese" 2 "Chinese" 3 "Both"
label values natl_id natl_id

* Recode v12 (Overall impression of China)
recode v12 (1=5) (2=4) (3=2) (4=1) (5/97=3) (98=.), gen (china_image)
label variable china_image "Overall impression of China"
label define image 1 "Very poor" 2 "Poor" 3 "Average/DK" 4 "Good" 5 "Very good"
label values china_image image
recode v12 (1/2=3) (3/4=1) (5/97=2) (98=.), gen (china_image2)
label variable china_image2 "Overall impression of China"
label define image2 1 "Poor/Very poor" 2 "Average/DK" 3 "Good/Very good"
label values china_image2 image2

* Recode v30 (Friend (control condition))
recode v30 (1=3) (2=2) (3=1) (4=0) (94/98=.), gen (friend_base)
label variable friend_base "Friend (control)"
* Recode v31 (Enemy (treatment condition))
recode v31 (1=4) (2=3) (3=2) (4=1) (5=0) (94/98=.), gen (enemy_test)
label variable enemy_test "Enemy (treatment)"
* Recode v32 (Enemy (control condition))
recode v32 (1=3) (2=2) (3=1) (4=0) (94/98=.), gen (enemy_base)
label variable enemy_base "Enemy (control)"
* Recode v33 (Friend (treatment condition))
recode v33 (1=4) (2=3) (3=2) (4=1) (5=0) (94/98=.), gen (friend_test)
label variable friend_test "Friend (treatment)"

* Creating list experiment variables
gen t=.
replace t=0 if friend_test<5
replace t=1 if enemy_test<5
label variable t "Experiment condition: test"
label define t 0 "friend treatment" 1 "enemy treatment"
label values t t

gen b=.
replace b=0 if friend_base<4
replace b=1 if enemy_base<4
label variable b "Experiment condition: control"
label define b 0 "friend control" 1 "enemy control"
label values b b

gen s=.
replace s=0 if ((b==0) & (t==1))
replace s=1 if ((b==1) & (t==0))
label variable s "Type of sample"
label define s 0 "Half-sample A" 1 "Half-sample B"
label values s s

gen cat_friend=.
replace cat_friend=0 if friend_base<4
replace cat_friend=1 if friend_test<5
label variable cat_friend "Experiment category: friend"
label define cat_friend 0 "friend_control" 1 "friend_treatment"
label values cat_friend cat_friend

gen cat_enemy=.
replace cat_enemy=0 if enemy_base<4
replace cat_enemy=1 if enemy_test<5
label variable cat_enemy "Experiment category: enemy"
label define cat_enemy 0 "enemy_control" 1 "enemy_treatment"
label values cat_enemy cat_enemy

gen friend_count=friend_base
replace friend_count=friend_test if mi(friend_base)
label variable friend_count "Item count variable for China as a friend"
gen enemy_count=enemy_base
replace enemy_count=enemy_test if mi(enemy_base)
label variable enemy_count "Item count variable for China as an enemy"

* Direct question
* Recode v34 (direct question)
recode v34 (1=1) (2=2) (3=3) (4=4) (5/97=5) (98=.), gen (dq1)
label variable dq1 "Perception of China"
label define dq1 1 "Friend" 2 "Enemy" 3 "Both" 4 "Neither" 5 "Other answers/DK"
label values dq1 dq1
recode v34 (1=1) (2=2) (3/97=3) (98=.), gen (dq2)
label variable dq2 "Perception of China"
label define dq2 1 "Friend" 2 "Enemy" 3 "Other/DK"
label values dq2 dq2

recode v34 (1=1) (2/97=0) (98=.), gen (dq_friend)
label variable dq_friend "Direct key friend"
label define dq_friend 1 "China is a friend" 0 "China is not a friend"
label values dq_friend dq_friend
recode v34 (2=1) (1 3 4 5/97=0) (98=.), gen (dq_enemy)
label variable dq_enemy "Direct key enemy"
label define dq_enemy 1 "China is an enemy" 0 "China is not an enemy"
label values dq_enemy dq_enemy

gen amb=.
replace amb=0 if ((dq2==1) & (china_image2==3))
replace amb=1 if ((dq2==2) & (china_image2==1))
replace amb=2 if ((dq2==1) & (china_image2<3))
replace amb=2 if ((dq2==2) & (china_image2>1))
replace amb=2 if (dq2==3)
label variable amb "Level of Ambivalence-toward-China"
label define amb 0 "China-positive" 1 "China-negative" 2 "China-ambivalent"
label values amb amb
tab dq2 china_image2, cell


* Summary statistics
sum female age3 education2 ethnicity partyid natl_id friend_base friend_test enemy_base enemy_test cat_friend cat_enemy friend_count enemy_count
  
  
// Tables 1 and 2 (March 2019 survey results) //
  
***** Difference-in-means estimator *****
ttest friend_test == friend_base, unpaired unequal welch
ttest enemy_test == enemy_base, unpaired unequal welch

* alternate method 
ttest friend_count, by(cat_friend)
ttest enemy_count, by(cat_enemy)


// Tables 4 and 5 (March 2019 survey results) //
***** Imai's ML Estimator *****
* Note: substantial computation time

* Friend list experiment 
est clear
kict ml friend_count female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb, nn(3) cond(cat_friend) est(imai) protect(50) iterate(100) rseed(123)
est store friend
est table friend, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)

* Enemy list experiment
kict ml enemy_count female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb, nn(3) cond(cat_enemy) est(imai) protect(50) iterate(100) rseed(123)
est store enemy
est table enemy, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)
est table friend enemy, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)



// Figures 3 and 4 //

// Figure 3 (left panel) //

reg friend_count cat_friend
eststo qexperiment_friend: margins, dydx(cat_friend) post coefl
eststo qdirect_friend: reg dq_friend 
coefplot qexperiment_friend qdirect_friend, ///
	name(maineffects1, replace) saving(19friend_levsdq.gph, replace) ///
	title("", margin(medlarge)) legend(off) vertical recast(bar) barwidth(0.75) ///
	ciopts(recast(rcap) lwidth(medthick)) citop format(%9.0f) scheme(s2color) ///
	coeflabels(cat_friend = "List experiment" _cons = "Direct question") ///
	ytitle("Estimated proportion", size(medium)) xtitle("") title("2019 March", size(medium)) ///
	yscale(range(.2 (.1) .7)) ylab(.2 (.1) .7, format(%9.1f))
graph display , ysize(1) xsize(1) scale(1) 


// Figure 4 (left panel) //

reg enemy_count cat_enemy
eststo qexperiment_enemy: margins, dydx(cat_enemy) post coefl
eststo qdirect_enemy: reg dq_enemy
coefplot qexperiment_enemy qdirect_enemy, ///
	name(maineffects2, replace) saving(19enemy_levsdq.gph, replace) ///
	title("", margin(medlarge)) legend(off) vertical recast(bar) barwidth(0.75) ///
	ciopts(recast(rcap) lwidth(medthick)) citop format(%9.0f) scheme(s2color) ///
	coeflabels(cat_enemy = "List experiment" _cons = "Direct question") ///
	ytitle("Estimated proportion", size(medim)) xtitle("") title("2019 March", size(medium)) ///
	yscale(range(0 (.1) .7)) ylab(0 (.1) .7, format(%9.1f))
graph display , ysize(1) xsize(1) scale(1) 



***** Appendix *****

// Table A.2.1 //
* Distribution of list experiment responses

tab friend_count cat_friend, col
tab enemy_count cat_enemy, col



*** Treatment randomization (balance tests) ***

// Table A.3.2.1 //
* Friend list experiment
eststo clear
eststo overall: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, columns(statistics) stats(mean sd) missing  
eststo bygroup: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, by(cat_friend) columns(statistics) stats(mean sd) missing  nototal
esttab overall bygroup,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps nonumber varwidth(15) replace wide 
eststo pvalues: quietly estpost ttest female age3 education2 ethnicity partyid natl_id amb, by(cat_friend) 
esttab pvalues, main(b %9.2f) aux(p %9.2f) wide // p-values

// Table A.3.2.2 //
* Enemy list experiment
eststo clear
eststo overall: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, columns(statistics) stats(mean sd) missing  
eststo bygroup: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, by(cat_enemy) columns(statistics) stats(mean sd) missing  nototal
esttab overall bygroup,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps nonumber varwidth(15) replace wide 
eststo pvalues: quietly estpost ttest female age3 education2 ethnicity partyid natl_id amb, by(cat_enemy) 
esttab pvalues, main(b %9.2f) aux(p %9.2f) wide // p-values


*** Test for no design effects ***
*use kict deff to estimate the joint probabilities of the answers to the key and nonkey items.

// Table A.3.3.1 //
* Friend list experiment
kict deff friend_count, nnonkey(3) condition(cat_friend) test gms nsim(10000)

// Table A.3.3.2 //
* Enemy list experiment
kict deff enemy_count, nnonkey(3) condition(cat_enemy) test gms nsim(10000)


*** Indirect test for no liars assumption (Floor effects for China as Enemy) ***

// Table A.3.4.1 //
recode enemy_base (0=1) (1/3=0), gen (enemyb_floor)
logit enemyb_floor female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb
est store enemyb_floor
est table enemyb_floor, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)



// Figures A.4 and A.5 //

* Figure A.4 (left panel)
tabplot amb partyid, percent yreverse height(.5) sep(partyid) bar1(bcolor(blue*0.5)) bar2(bcolor(green*0.5)) ///
  showval subtitle(2019 March, pos(1)) saving(19partyidxamb.gph, replace)
  
* Figure A.5 (left panel)
tabplot amb natl_id, percent yreverse height(.5) sep(natl_id) bar1(bcolor(green*0.5)) bar2(bcolor(red*0.5)) bar3(bcolor(blue*0.5)) ///
  showval subtitle(2019 March, pos(1)) saving(19natl_idxamb.gph, replace) 
